#import "dfhgwhruebvhrheysternideology.h"
#import <YYText/YYText.h>
#import "dfhgwhruebvhrheyluggagerecorder.h"
#import "dfhgwhruebvhrheythrivealternate.h"
#import "dfhgwhruebvhrheybangorganismViewController.h"
#import "dfhgwhruebvhrheyroomcatalogViewController.h"
#import "dfhgwhruebvhrheyquantifysignifyViewController.h"
@interface dfhgwhruebvhrheyroomcatalogViewController ()
@property(nonatomic, copy) NSString *mobile;
@property (nonatomic, strong) UIButton *agreeBtn;
@end
@implementation dfhgwhruebvhrheyroomcatalogViewController {
    UIView *bgView;
    UIImageView *icon;
    UIScrollView *scrollView;
    UIView *containerView;
    UILabel *phoneText;
    dfhgwhruebvhrheythrivealternate *phone;
    YYLabel *agreeText;
    UIButton *continueBtn;
}
- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputTextChange:) name:UITextFieldTextDidChangeNotification object:nil];
}
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
	[self loadUI];
}
- (void)loadUI {
	[self configBackIcon:@"dhgfywegwygeydwcurvereciprocal"];
    bgView = [[UIView alloc] init];
    bgView.layer.cornerRadius = 20;
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    icon = [[UIImageView alloc] init];
    icon.image = [UIImage imageNamed:@"dhgfywegwygeydwpresideabsolute"];
    [bgView addSubview:icon];
    scrollView = [[UIScrollView alloc] init];
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:scrollView];
    containerView = [[UIView alloc] init];
    containerView.backgroundColor = white_color.dhgfywegwygeydwlearneddesignate;
    [scrollView addSubview:containerView];
    phoneText = [[UILabel alloc] init];
    phoneText.text = @"Phone number";
    phoneText.font = [UIFont regular:16];
    phoneText.textColor = text_color1.dhgfywegwygeydwlearneddesignate;
    [containerView addSubview:phoneText];
    phone = [[dfhgwhruebvhrheythrivealternate alloc] init];
    [containerView addSubview:phone];
    phone.left_text = @"+63";
    phone.tag = 100;
    phone.rightBtn = nil;
    phone.placeholder = @"Please Enter Phone Number";
	self.agreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	self.agreeBtn.tag = 100;
	[self.agreeBtn setImage:[UIImage imageNamed:@"dhgfywegwygeydwfeasibleveto"] forState:UIControlStateNormal];
	[self.agreeBtn setImage:[UIImage imageNamed:@"dhgfywegwygeydwvetobulletin"] forState:UIControlStateSelected];
	[self.agreeBtn addTarget:self action:@selector(btnActions:) forControlEvents:UIControlEventTouchUpInside];
	[containerView addSubview:self.agreeBtn];
	NSString *string = @"I have read and agree to the \"Privacy Polincy\"";
	NSMutableAttributedString *matt = [[NSMutableAttributedString alloc] initWithString:string];
	NSRange r1 = [string rangeOfString:@"\"Privacy Polincy\""];
	matt.yy_font = [UIFont regular:13];
	matt.yy_color = text_color1.dhgfywegwygeydwlearneddesignate;
	[matt yy_setTextHighlightRange:r1 color:primary_color.dhgfywegwygeydwlearneddesignate backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
		[self protocalClick:100];
	}];
	agreeText = [[YYLabel alloc] init];
	agreeText.numberOfLines = 0;
	agreeText.attributedText = matt;
	[containerView addSubview:agreeText];
	agreeText.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    continueBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    continueBtn.tag = 101;
    continueBtn.layer.cornerRadius = 10;
    continueBtn.titleLabel.font = [UIFont medium:16];
    continueBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
    [continueBtn setTitle:@"Continue" forState:UIControlStateNormal];
    [continueBtn setTitleColor:white_color.dhgfywegwygeydwlearneddesignate forState:UIControlStateNormal];
    [continueBtn addTarget:self action:@selector(btnActions:) forControlEvents:UIControlEventTouchUpInside];
    [containerView addSubview:continueBtn];
}
- (void)inputTextChange:(NSNotification *)not {
	UITextField *input = [not object];
	if ([input.text hasPrefix:@"8"] || [input.text hasPrefix:@"9"]) {
		if (input.text.length >= 10) {
			phone.content = [input.text substringToIndex:10];
			[self.view endEditing:YES];
        }else {
            phone.content = input.text;
        }
	}else {
		if (input.text.length >= 11) {
			phone.content = [input.text substringToIndex:11];
			[self.view endEditing:YES];
        }else {
            phone.content = input.text;
        }
	}
}
- (void)protocalClick:(int)type {
	switch (type) {
		case 100:{ 
			__weak typeof(self) weakSelf = self;
			dfhgwhruebvhrheybangorganismViewController *webVC = [[dfhgwhruebvhrheybangorganismViewController alloc] init];
			webVC.bottomTitle = @"Aceptar";
			webVC.isShowBottomView = YES;
            webVC.title = @"Privacy Policy";
			[webVC loadURLString:privateProtocalH5];
			[self.navigationController pushViewController:webVC animated:YES];
			[webVC bottomBtnsClick:^(UIButton * _Nonnull sender) {
				__strong typeof(weakSelf) strongSelf = weakSelf;
				strongSelf.agreeBtn.selected = YES;
			}];
		}
			break;
		default:
			break;
	}
}
- (void)checkPhoneNumber {
	[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
	__weak typeof(self) weakSelf = self;
	NSDictionary *params = @{@"mobile": self.mobile, @"pgid": @"1"};
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwlambbreast path:checkNewMobileExsitApi isEncrypt:NO withParams:params success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
		__strong typeof(weakSelf) strongSelf = weakSelf;
		dfhgwhruebvhrheysternideology *model = [dfhgwhruebvhrheysternideology yy_modelWithJSON:json];
        if (model.code == 201) {
            dfhgwhruebvhrheyquantifysignifyViewController *confirmVC = [[dfhgwhruebvhrheyquantifysignifyViewController alloc] init];
            confirmVC.mobile = strongSelf.mobile;
            [strongSelf.navigationController pushViewController:confirmVC animated:YES];
        }else {
            [dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
        }
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
	}];
}
- (void)btnActions:(UIButton *)sender {
    switch (sender.tag) {
        case 100: {
            sender.selected = !sender.selected;
        }
            break;
        case 101: {
            if (phone.content.length == 0) {
                [self.view show:@"Mobile number cannot be empty"];
                return;
            }
            if (![phone.content dhgfywegwygeydwdesignatecommence]) {
                [self.view show:@"Please enter the correct format for the phone number，which must start with 9, 09, 8 or 08"];
                return;
            }
            if (!self.agreeBtn.isSelected) {
                [self.view show:@"Please read and agree to the agreement"];
                return;
            }
            self.mobile = phone.content;
            if ([self.mobile hasPrefix:@"0"]) {
                self.mobile = [self.mobile substringFromIndex:1];
            }
            [self checkPhoneNumber];
        }
            break;
        default:
            break;
    }
}
- (void)viewDidLayoutSubviews {
	[super viewDidLayoutSubviews];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(48);
        make.left.bottom.right.equalTo(self.view);
    }];
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bgView);
        make.top.equalTo(bgView).offset(-40);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(icon.mas_bottom).offset(16);
        make.left.bottom.right.equalTo(bgView);
    }];
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.width.equalTo(scrollView);
    }];
    [phoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(168, 24));
        make.top.equalTo(containerView).offset(16);
        make.left.equalTo(containerView).offset(12);
    }];
    [phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(56);
        make.centerX.equalTo(containerView);
        make.left.equalTo(containerView).offset(12);
        make.top.equalTo(phoneText.mas_bottom).offset(12);
    }];
    [self.agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.left.equalTo(containerView).offset(12);
        make.top.equalTo(phone.mas_bottom).offset(22);
    }];
    [agreeText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(21);
        make.centerY.equalTo(self.agreeBtn);
        make.right.equalTo(self.view).offset(-12);
        make.left.equalTo(self.agreeBtn.mas_right).offset(12);
    }];
    [continueBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(56);
        make.centerX.bottom.equalTo(containerView);
        make.left.equalTo(containerView).offset(12);
        make.top.equalTo(self.agreeBtn.mas_bottom).offset(42);
    }];
}
@end
